package com.tcsl.mylibrary.utils;

import java.math.BigDecimal;

/**
 * 关于金额以及百分比的四则运算
 * <p>
 * 因为Float类型和Double类型在进行四则运算的时候会出现精度损失的情况，使用BigDecimal类进行运算
 *
 * @author 王伟
 *         <p>
 *         AmountCalculation
 */
public class AC {
    /**
     * 乘法
     *
     * @param strMultiplicand 被乘数
     * @param strMultiplier   乘数
     * @return 结果
     */
    public static String amountMultiply(Object objMultiplicand,
                                        Object objMultiplier) {
        String result[] = formatCheck(objMultiplicand, objMultiplier);
        String strMultiplicand = result[0];
        String strMultiplier = result[1];
        BigDecimal multiplicant = new BigDecimal(strMultiplicand);
        BigDecimal multiplier = new BigDecimal(strMultiplier);
        return multiplicant.multiply(multiplier).toString();
    }

    public static Float amountMultiplyToFloat(Object objMultiplicand,
                                              Object objMultiplier) {
        String result[] = formatCheck(objMultiplicand, objMultiplier);
        String strMultiplicand = result[0];
        String strMultiplier = result[1];
        BigDecimal multiplicant = new BigDecimal(strMultiplicand);
        BigDecimal multiplier = new BigDecimal(strMultiplier);
        return Float.valueOf(multiplicant.multiply(multiplier).toString());
    }

    public static Double amountMultiplyToDouble(Object objMultiplicand,
                                                Object objMultiplier) {
        String result[] = formatCheck(objMultiplicand, objMultiplier);
        String strMultiplicand = result[0];
        String strMultiplier = result[1];
        BigDecimal multiplicant = new BigDecimal(strMultiplicand);
        BigDecimal multiplier = new BigDecimal(strMultiplier);
        return Double.valueOf(multiplicant.multiply(multiplier).toString());
    }

    /**
     * 除法
     *
     * @param strDividend 被除数
     * @param strDivisor  除数
     * @return 结果 如果除数为0 则返回0
     */
    public static String amountDivide(Object objDividend, Object objDivisor) {
        String result[] = formatCheck(objDividend, objDivisor);
        String strDividend = result[0];
        String strDivisor = result[1];
        if ("0".equals(strDivisor)) {
            return "0";
        }
        BigDecimal dividend = new BigDecimal(strDividend);
        BigDecimal divisor = new BigDecimal(strDivisor);
        return dividend.divide(divisor).toString();
    }

    public static Float amountDivideToFloat(Object objDividend,
                                            Object objDivisor) {
        String result[] = formatCheck(objDividend, objDivisor);
        String strDividend = result[0];
        String strDivisor = result[1];
        if ("0".equals(strDivisor)) {
            return 0f;
        }
        BigDecimal dividend = new BigDecimal(strDividend);
        BigDecimal divisor = new BigDecimal(strDivisor);
        return Float.valueOf(dividend.divide(divisor).toString());
    }

    public static Double amountDivideToDouble(Object objDividend,
                                              Object objDivisor) {
        String result[] = formatCheck(objDividend, objDivisor);
        String strDividend = result[0];
        String strDivisor = result[1];
        if ("0".equals(strDivisor)) {
            return 0d;
        }
        BigDecimal dividend = new BigDecimal(strDividend);
        BigDecimal divisor = new BigDecimal(strDivisor);
        return Double.valueOf(dividend.divide(divisor).toString());
    }

    /**
     * 加法
     *
     * @param strAddend1 被加数
     * @param strAddend2 加数
     * @return 结果
     */
    public static String amountPlus(Object objAddend1, Object objAddend2) {
        String result[] = formatCheck(objAddend1, objAddend2);
        String strAddend1 = result[0];
        String strAddend2 = result[1];
        BigDecimal addend1 = new BigDecimal(strAddend1);
        BigDecimal addend2 = new BigDecimal(strAddend2);
        return addend1.add(addend2).toString();
    }

    public static Float amountPlusToFloat(Object objAddend1, Object objAddend2) {
        String result[] = formatCheck(objAddend1, objAddend2);
        String strAddend1 = result[0];
        String strAddend2 = result[1];
        BigDecimal addend1 = new BigDecimal(strAddend1);
        BigDecimal addend2 = new BigDecimal(strAddend2);
        return Float.valueOf(addend1.add(addend2).toString());
    }

    public static Double amountPlusToDouble(Object objAddend1, Object objAddend2) {
        String result[] = formatCheck(objAddend1, objAddend2);
        String strAddend1 = result[0];
        String strAddend2 = result[1];
        BigDecimal addend1 = new BigDecimal(strAddend1);
        BigDecimal addend2 = new BigDecimal(strAddend2);
        return Double.valueOf(addend1.add(addend2).toString());
    }

    /**
     * 减法
     *
     * @param strAddend1 被减数
     * @param strAddend2 减数
     * @return 结果
     */
    public static String amountSub(Object objMinuend, Object objSubtractor) {
        String result[] = formatCheck(objMinuend, objSubtractor);
        String strMinuend = result[0];
        String strSubtractor = result[1];
        BigDecimal minuend = new BigDecimal(strMinuend);
        BigDecimal subtractor = new BigDecimal(strSubtractor);
        return minuend.subtract(subtractor).toString();
    }

    public static Float amountSubToFloat(Object objMinuend, Object objSubtractor) {
        String result[] = formatCheck(objMinuend, objSubtractor);
        String strMinuend = result[0];
        String strSubtractor = result[1];
        BigDecimal minuend = new BigDecimal(strMinuend);
        BigDecimal subtractor = new BigDecimal(strSubtractor);
        return Float.valueOf(minuend.subtract(subtractor).toString());
    }

    public static Double amountSubToDouble(Object objMinuend,
                                           Object objSubtractor) {
        String result[] = formatCheck(objMinuend, objSubtractor);
        String strMinuend = result[0];
        String strSubtractor = result[1];
        BigDecimal minuend = new BigDecimal(strMinuend);
        BigDecimal subtractor = new BigDecimal(strSubtractor);
        return Double.valueOf(minuend.subtract(subtractor).toString());
    }

    /**
     * 输入格式检测
     * <p>
     * 如果待检测的数为空字符串 则返回“0”
     * <p>
     * 如果a或b某一个为null 则均返回“0”“0”(视为无效运算)
     * <p>
     * 如果待检测的数不是String,Integer,Float,Double的其中一种则返回“0”
     * <p>
     * 否则结果返回为转换后的字符串
     *
     * @param a 待检测是数a
     * @param b 待检测的数b
     * @return c 结果
     */
    private static String[] formatCheck(Object a, Object b) {
        String[] c = new String[]{"0", "0"};
        if (a == null || b == null) {
            return c;
        }
        if (a instanceof String) {
            if (!"".equals(a)) {
                c[0] = (String) a;
            }
        } else if (a instanceof Integer || a instanceof Float
                || a instanceof Double) {
            c[0] = String.valueOf(a);
        } else {
            c[0] = "0";
        }
        if (b instanceof String) {
            if (!"".equals(b)) {
                c[1] = (String) b;
            }
        } else if (b instanceof Integer || b instanceof Float
                || b instanceof Double) {
            c[1] = String.valueOf(b);
        } else {
            c[1] = "0";
        }
        return c;
    }
}
